﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using JinianNet.JNCms.Filters;
using JinianNet.JNCms.Models;

namespace JinianNet.JNCms.Controllers
{
    [ManagerFilter()]
    public class ClassifyController : ControllerBase
    {
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Classify model)
        {
            Data.DataAccess.CreateClassify().Update(model);
            return Content(Config.GetTips("恭喜，分类修改成功", Url.Action("List", "Classify", new { item = model.ClassType }), true));
        }


        public ActionResult Edit(int item = 0)
        {
            if (item > 0)
            {
                JinianNet.JNCms.Data.IClassify dal = Data.DataAccess.CreateClassify();
                Classify model = Data.DataAccess.CreateClassify().GetItem(item);
                if (model != null)
                {
                    if (model.ParentID > 0)
                    {

                        Classify category = dal.GetItem(model.ParentID);
                        if (category == null)
                        {
                            ViewBag.ParentName = category.Title;
                        }
                        else
                        {
                            ViewBag.ParentName = "根目录";

                        }
                    }
                    else
                    {
                        ViewBag.ParentName = "根目录";
                    }
                    return View(model);
                }
            }
            return PageNotFound();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Insert(Classify model)
        {
            model.CreateTime = DateTime.Now;
            if (model.ParentID > 0)
            {
                Classify category = Data.DataAccess.CreateClassify().GetItem(model.ParentID);
                if (category == null)
                {
                    return Content(Config.GetTips("对不起，父类别不存在！", null, false));
                }
                else
                {
                    model.ClassType = category.ClassType;
                }
            }

            Data.DataAccess.CreateClassify().Insert(model);
            return Content(Config.GetTips("恭喜，类别添加成功", Url.Action("List", "Classify", new { item = model.ClassType }), true));
        }

        public ActionResult Insert(int item = 0)
        {
            Classify model = new Classify();
            model.CreateTime = DateTime.Now;
            model.ParentID = 0;
            model.ClassType = item;
            ViewBag.ParentName = "根目录";
            return View("Edit", model);
        }

        public ActionResult Delete(string item)
        {
            Data.IClassify dal = Data.DataAccess.CreateClassify();
            if (item.IndexOf(',') < 0)
                dal.Delete(int.Parse(item));
            else
                foreach (string value in item.Split(','))
                    if (!string.IsNullOrEmpty(value))
                        dal.Delete(int.Parse(value));
            return Content(Config.GetTips("操作成功", null, true));
        }

        public ActionResult List(int item)
        {
            return View(Data.DataAccess.CreateClassify().GetList(0, item));
        }
    }
}
